from unittest import TestCase
from ddt import ddt, data, unpack
from time import sleep

from selenium.webdriver.common.by import By

from TEST.Data.LoanData import LoanData
from TEST.FuncLogic.PageLogin import Login
from TEST.FuncLogic.PageLoan import Loan
from TEST.FuncLogic.Page import Page
from TEST.FuncLogic.Page import Qscreen
from TEST.Head.数据准备 import readyMysql

@ddt
class TestLoan(TestCase):
    readyMysql().delete()
    readyMysql().create()

    @data(*LoanData().data()[:5]+LoanData().data()[6:])
    @unpack
    def test_Sucess(self, username, password, payword, loan, time, expect):
        # driver信息
        driver = Page().driver
        # 登陆
        Login(driver).login(username, password)
        sleep(0.5)
        Loan(driver).loan(loan, time)
        sleep(0.5)
        # 获取结果信息
        result = driver.switch_to.active_element.text
        result = result[:result.find('\n')]
        # 判断
        Qscreen(driver, expect, result)
        print(result ,expect)
        # 关闭页面
        driver.quit()

        self.assertEqual(expect, result)

    @data(*LoanData().data()[5:6])
    @unpack
    def test_6(self, username, password, payword, loan, time, expect):
        # driver信息
        driver = Page().driver
        # 登陆
        Login(driver).login(username, password)
        sleep(0.5)
        # 点击金融工具
        driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[3]/a').click()
        # 点击安全网贷
        driver.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[3]/ul/li[2]/a').click()
        # 输入贷款金额
        driver.find_element(By.ID, 'amount').send_keys(loan)
        # 输入贷款期限/天
        driver.find_element(By.ID, 'term').send_keys(time)
        # 点击申请
        driver.find_element(By.ID, 'submit').click()
        sleep(1)
        # 获取结果信息
        result = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/span').text
        # 判断
        Qscreen(driver, expect, result)
        # 关闭页面
        driver.quit()

        self.assertEqual(expect, result)


